/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.service.impl;

import cn.hutool.core.util.StrUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.punish.mapper.CasePartiesRecordMapper;
import com.icinfo.cloud.provider.punish.service.ICasePartiesRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CasePartiesRecord;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CasePartiesRecordVo;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 描述: 案件当事人表-TRJ case_parties_info 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年02月15日
 */
@Service
@Slf4j
public class CasePartiesRecordServiceImpl extends MyBatisServiceSupport implements ICasePartiesRecordService {

    @Resource
    private CasePartiesRecordMapper casePartiesRecordMapper;


    /**
     * 【V1.0.0】描述：根据案件id查询案件当事人信息
     *
     * @param caseId 案件id
     * @return {@link CasePartiesRecordVo} 案件当事人信息
     * @author xiangyuyu
     * @date 2022-02-18 16:02
     */
    @Override
    public List<CasePartiesRecordVo> getCasePartiesInfo(String caseId) {
        log.info("getCasePartiesInfo start(caseId:+" + caseId + ")...");
        Example queryExample = new Example(CasePartiesRecord.class);
        queryExample.createCriteria().andEqualTo("caseId", caseId).andEqualTo("status", Constant.STRING_1);
        queryExample.setOrderByClause(" party_type desc,create_time asc ");
        List<CasePartiesRecord> casePartiesRecord = casePartiesRecordMapper.selectByExample(queryExample);
        if (ObjectUtils.isEmpty(casePartiesRecord) || casePartiesRecord.size() == 0) {
            throw new BusinessException("未找到当事人信息");
        } else {
            List<CasePartiesRecordVo> parties = new ArrayList<>();
            casePartiesRecord.forEach(e -> {
                CasePartiesRecordVo party = new CasePartiesRecordVo();
                BeanUtils.copyProperties(e, party);
                parties.add(party);
            });
            log.info("getCasePartiesInfo end-parties:" + parties);
            return parties;
        }
    }

    @Override
    public List<CasePartiesRecord> getCasePartiesInfoByCaseId(String caseId) {
        if (StrUtil.isBlank(caseId)) {
            throw new BusinessException("当事人记录查询失败，案件id不能为空");
        }
        Example queryExample = new Example(CasePartiesRecord.class);
        queryExample.createCriteria().andEqualTo("caseId", caseId).andEqualTo("status", Constant.STRING_1);
        queryExample.setOrderByClause(" party_type desc,create_time asc ");
        return casePartiesRecordMapper.selectByExample(queryExample);
    }

}
